package com.longsunhd.yum.buffalo.network.action;

import android.os.Handler;
import android.os.Message;

import com.longsunhd.yum.buffalo.base.BaseApplication;
import com.longsunhd.yum.buffalo.bean.LoginBean;
import com.longsunhd.yum.buffalo.bean.UserInfoBean;
import com.longsunhd.yum.buffalo.network.Network;


import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * 作者：黄宣淇
 * 时间：2021/5/7 0007
 * 如何代码没有备注，一个月后，只有你和上帝知道--鲁迅
 */
public class UserApiUtils  {
   private    Message message=new Message();
    static UserApiUtils  instance;
    public static UserApiUtils newInstance(){

       instance = new UserApiUtils();

        return instance;
    }


    /*登录*/
    public  void loginMap(Map<String ,String >map, final Handler handler){
        Network.getAccountApi()
                .onLoginMap(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<LoginBean>() {
                    @Override
                    public void accept(final LoginBean loginBean) throws Exception {
                        if(loginBean!=null){
                            message.obj=loginBean;
                            handler.sendMessage(message);
                        }else {
                            BaseApplication.showToast("登录失败");
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.getMessage();
                        BaseApplication.showToast("登录失败");
                    }
                });
    }
    /*获取用户信息*/
    public  void getUserInfo(String token,final Handler handler){
        Network.getAccountApi(token)
                .getMyInformation()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<UserInfoBean>() {
                    @Override
                    public void accept(final UserInfoBean loginBean) throws Exception {
                        if(loginBean!=null){
                            message.obj=loginBean;
                            handler.sendMessage(message);
                        }else {
                            BaseApplication.showToast("没有个人信息");
                        }

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.getMessage();
                        BaseApplication.showToast("获取用户信息出现错误");
                    }
                });
    }
}
